import { createRouter, createWebHistory } from 'vue-router'
import {userStore} from '@/stores/counter.js'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
  {
  path:'/',
  name:'welcome',
  component:()=>import('@/views/WelcomeVue.vue'),
  children:[
  {
  path:'/register',
  name:'welcome-register',
  component:()=>import('@/components/welcome/RegisterPage.vue')
  },{
    path:'',
    name:'welcome-login',
    component:()=>import('@/components/welcome/LoginPage.vue')
  },{
      path:'/emailLogin',
      name:'emailLogin',
      component:()=>import('@/components/welcome/EmailLoginPage.vue')
  }
  ]
  },{
  path:'/index',
  name:'index',
  component:()=>import('@/views/indexVue.vue'),
  children:[
  {
      path:'/own',
      name:'own',
      component:()=>import('@/views/OwnVue.vue')
    },{
          path:'/stuInfo',
          name:'stuInfo',
          component:()=>import('@/views/stuInfo.vue')
    },{
              path:'/course',
              name:'course',
              component:()=>import('@/views/CourseVue.vue')
    },{
                  path:'/score',
                  name:'score',
                  component:()=>import('@/views/ScoreVue.vue')
    },{
                      path:'/warn',
                      name:'warn',
                      component:()=>import('@/views/WarningVue.vue')

    },{
                          path:'/total',
                          name:'total',
                          component:()=>import('@/views/TotalVue.vue')


    },{
                                path:'/college',
                                name:'college',
                                component:()=>import('@/views/CollegeMajorVue.vue')


          }
  ]
}
  ]
})

router.beforeEach((to,from,next)=>{
const store=userStore()
if(store.auth.user!=null&&to.name.startsWith('welcome-')){
next('/index')
}else if(store.auth.user==null&&to.fullPath.startsWith('/index')){
next('/')
}else if(to.matched.length==0){
next('/index')
}else{
next()
}
})
export default router
